// Text of project SoundTricks written on 11/21/95 at 8:17 PM
// Beginning of text file Constants
// copyright  1993-1995 by Apple Computer, Inc.  All rights reserved.

OpenResFile(home & "resources");
DefConst('kOurSound, GetSound11("Droplet"));
CloseResFile();
// End of text file Constants
// Beginning of file SoundTricks.t

// Before Script for "mainView"
// copyright  1993-1995 by Apple Computer, Inc.  All rights reserved.


mainView :=
    {viewBounds: {left: -12, top: 49, right: 106, bottom: 199},
     viewSetupFormScript:
       func()
       begin
       
          if not originalSound then
          	originalSound := kOurSound; 
       
          if not twiceSpeedSound then 
          begin
       	   // Note that there are currently only two allowed rates which you
       	   // can import sound resources at...11 and 22 kHz
       	   // Anything else will not work.
       	   // However, you can manipulate the samplingRate to change pitch dynamically.
       
       		twiceSpeedSound := Clone(kOurSound); // Conveniently gets the sound.
       		twiceSpeedSound.samplingRate := twiceSpeedSound.samplingRate * 2; // Changes speed and pitch by 2
          end;
       
       	/*
       		// could set up the backward sound here directly.  Or, could do it in a deferred action
       		// so that the main view was visible immediately.  However, if we do this, should provide
       		// some visible indication that there is setting up happening.
       		// Instead, look at the playBackwardSound button.  (We defer setting up until the sound is
       		// required the first time...
       		
       	   if not backWardSound then
       	   	AddDeferredAction(func(context) context:SetupBackwardsSound(), [self]);
       	
       	*/
       end,
     originalSound: nil,
     twiceSpeedSound: nil,
     backwardSound: nil,
     viewFlags: 581,
     declareSelf: 'base,
     viewClickScript:
       func(unit)
       begin
            self:Drag(unit,nil);
            return nil; //We haven't really handled the click
       end,
     viewQuitScript:
       func()
       begin
       	// it's bad form to leave large structures in RAM when
       	// the app is closed.  If they must be kept around, stuff
       	// them in a soup.  (We'll just nil them out.)
       	backwardSound := nil;
       	originalSound := nil;
       	twiceSpeedSound := nil;
       end,
     SetupBackwardsSound:
       func native ()
       begin
       	local int singleSample, place;
       	local int i;
       
       	backwardSound := DeepClone(kOurSound); 
       	// In Newton 2.0, the best way to do this is to create the samples object
       	// as a Virtual Binary Object to store the data to minimize NewtonScript memory 
       	// usage. Doing the DeepClone(...) stores it all in NewtonScript memory.
       	
       	
       	local int theSize := Length(backwardsound.samples) - 1; // subtract one because loop is 0-based.
       	local samples := backwardSound.samples;
       
       	local int timeCount := Ticks();  // gentlemen, start your engines!
       	
       	place := theSize;
       	for i := 0 to (theSize div 2) do  // samples are always multiples of 4 bytes long, so div 2 is safe.
       	begin
       		singleSample := ExtractByte(samples, i);
       		StuffByte(samples, i, ExtractByte(samples, place));
       		StuffByte(samples, place, singleSample);
       		place := place - 1;
       	end;
       	
       	Print("Reversing the sound samples took:" && (Ticks() - timeCount));
       end,
     debug: "mainView",
     _proto: @180
    };

_view000 :=
    {text: "Sound Tricks",
     viewBounds: {left: 27, top: 16, right: 94, bottom: 34},
     _proto: @218
    };
AddStepForm(mainView, _view000);



playOriginalSoundButton :=
    {text: "Play Sound",
     viewBounds: {left: 14, top: 42, right: 106, bottom: 62},
     buttonClickScript:
       func()
       begin
          PlaySound(originalSound);
       end,
     debug: "playOriginalSoundButton",
     _proto: @226
    };
AddStepForm(mainView, playOriginalSoundButton);
StepDeclare(mainView, playOriginalSoundButton, 'playOriginalSoundButton);



playTwiceSpeedSoundButton :=
    {text: "Play Twice Speed",
     viewBounds: {left: 14, top: 74, right: 106, bottom: 94},
     buttonClickScript:
       func()
       begin
          PlaySound(twiceSpeedSound);
       end,
     debug: "playTwiceSpeedSoundButton",
     _proto: @226
    };
AddStepForm(mainView, playTwiceSpeedSoundButton);
StepDeclare(mainView, playTwiceSpeedSoundButton, 'playTwiceSpeedSoundButton);



playBackwardSoundButton :=
    {text: "Play Backwards",
     viewBounds: {left: 14, top: 106, right: 106, bottom: 126},
     viewFlags: 515,
     buttonClickScript:
       func()
       begin
       	if not backwardSound then
       	begin
       		SetValue(self, 'text, "Setting up...");
       		RefreshViews();
       		:Parent():SetupBackwardsSound();
       		RemoveSlot(self, 'text);
       		:Dirty();
       		RefreshViews();
       	end;
       	
          PlaySound(backwardSound);
       end,
     debug: "playBackwardSoundButton",
     _proto: @226
    };
AddStepForm(mainView, playBackwardSoundButton);
StepDeclare(mainView, playBackwardSoundButton, 'playBackwardSoundButton);




constant |layout_SoundTricks.t| := mainView;
// End of file SoundTricks.t



